package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealOverViewVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        String name = setmeal.getName();
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmeal.setStatus(StatusConstant.ENABLE);

        setmealMapper.addSetmeal(setmeal);//返回不了，有点奇怪
        Long setmealId = setmealMapper.getSetmealId(name);//根据套餐名查出id

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();//关联表
        //新增
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
            setmealDishMapper.addSetmeal(setmealDish);
        }
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);//查出分页数
        List<SetmealVO> result = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, result);
    }

    @Override
    public void setStatus(Integer status, Long id) {
        setmealMapper.setStatus(status, id);
    }

    @Override
    public Setmeal getSeteamlById(Long id) {
        return setmealMapper.getSetmealById(id);
    }

    @Override
    public void updateSetmealInfo(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());

        setmealMapper.updateSetmealInfo(setmeal);//操作套餐表

        Long setmealId = setmealDTO.getId();
        ArrayList<Long> list = new ArrayList<>();
        list.add(setmealId);
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        //先将数据库关联的数据全部删除
        setmealDishMapper.removeSetmealDish(list);
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(setmealId);
            setmealDishMapper.addSetmeal(setmealDish);//添加
        }
    }

    @Override
    public void removeSetmealByIds(List<Long> ids) {
        setmealMapper.removeSetmealByIds(ids);
        setmealDishMapper.removeSetmealDish(ids);
    }

    @Override
    public List<Setmeal> list(Integer categoryId) {
        return setmealMapper.list(categoryId);
    }

    @Override
    public SetmealOverViewVO getSetmealOver() {
        Integer sold = setmealMapper.getSold();
        Integer disContinuedSold= setmealMapper.getDisContinuedSold();
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(disContinuedSold)
                .build();
    }

//    @Override
//    public Long getSetmealId(String name) {
//        return setmealMapper.getSetmealId(name);
//    }
}
